﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Data;
using CMSModel;
using System.Text;

public partial class Import_cpdb_ImportNews : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    

        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        StockInfoDAL s = new StockInfoDAL();
        NewsImportBLL importBll = new NewsImportBLL();
        if (Request["flag"] == "all")
        {
            var data = dal.CPTypeMatching.ToArray();
            foreach (var c in data)
            {
                var newsLst = s.NewsListByTypeId(c.CpTypeId, c.UTime);
                for (int i = newsLst.Rows.Count - 1; i >= 0; i--)
                {
                    importBll.Import((long)newsLst.Rows[i]["ob_textid_0022"], c.LocalTypeId);
                    c.UTime = Tools.GetDateTime(newsLst.Rows[i]["ob_rectime_0022"]);
                    dal.SaveChanges();
                }
            }
            var newsTime= dal.KeyValue.Where(x=>x.Key=="JCSTOCKNEWS").ToList();
            DateTime nTime=DateTime.MinValue;
            if(newsTime.Count>0)
            {
                 if(!DateTime.TryParse(newsTime[0].Value,out nTime))
                 {
                     dal.KeyValue.DeleteObject(newsTime[0]);
                     dal.SaveChanges();
                 }   
            }
            if(nTime==DateTime.MinValue)
            {
                nTime=DateTime.Now.AddDays(-1);
                if (!dal.KeyValue.Any(x => x.Key == "JCSTOCKNEWS"))
                {
                    KeyValue keyValue = new KeyValue();
                    keyValue.Key = "JCSTOCKNEWS";
                    keyValue.Value = DateTime.Now.AddDays(-1).ToString();
                    keyValue.Desc = "巨潮股票关联新闻更新时间";
                    dal.KeyValue.AddObject(keyValue);
                    dal.SaveChanges();
                }
            }
            DataTable tb= s.GetNewsIdReStocks(nTime);

            var keyValue2 = dal.KeyValue.First(x => x.Key == "JCSTOCKNEWS");
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                importBll.Import((long)tb.Rows[i]["OB_TEXTID_0021"], "1002");
                DateTime? upTime = Tools.GetDateTime(tb.Rows[i]["F004D_0021"]);
                if (upTime != null)
                {
                    var tempTime = DateTime.Parse(keyValue2.Value);
                    if (upTime > tempTime)
                    {
                        keyValue2.Value = upTime.ToString();
                        dal.SaveChanges();
                    }
                }
            }

        }
        else
        {
            string typeId = Request["LocalTypeId"];
            string cpTypeId = Request["CpTypeId"];
            var data = dal.CPTypeMatching.Where(x => x.LocalTypeId == typeId && x.CpTypeId == cpTypeId).First();
            var newsLst = s.NewsListByTypeId(data.CpTypeId, data.UTime);
            for (int i = newsLst.Rows.Count - 1; i >= 0; i--)
            {
                importBll.Import((long)newsLst.Rows[i]["ob_textid_0022"], typeId);
                data.UTime = Tools.GetDateTime(newsLst.Rows[i]["ob_rectime_0022"]);
                dal.SaveChanges();
            }
        }
        ClientScript.RegisterStartupScript(GetType(), "delete", "alert('导入完成');document.location='News.aspx'", true);
    }


}